package com.project.xsf.controller;


import com.alibaba.fastjson.JSONObject;
import com.project.common.entity.PageVo;
import com.project.xsf.entity.MApply;
import com.project.xsf.entity.MDesignProcedure;
import com.project.xsf.entity.MDesignProcedureDetails;
import com.project.xsf.entity.MDesignProcedureModule;
import com.project.xsf.mapper.MDesignProcedureDetailsMapper;
import com.project.xsf.service.MApplyService;
import com.project.xsf.service.MDesignProcedureDetailsService;
import com.project.xsf.service.MDesignProcedureModuleService;
import com.project.xsf.service.MDesignProcedureService;

import com.project.xy.entity.DFile;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.service.DFileService;
import com.project.xy.service.DModuleDetailsService;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/mDesignProcedure")
public class  MDesignProcedureController  {
    @Autowired
    private MDesignProcedureService mDesignProcedureService;
    @Autowired
    private DFileService dFileService;
    @Autowired
    private DModuleDetailsService dModuleDetailsService;
    @Autowired
    private MDesignProcedureModuleService mDesignProcedureModuleService;
    @Autowired
    private MDesignProcedureDetailsService mDesignProcedureDetailsService;
    @Autowired
    private MApplyService mApplyService;

    @PostMapping("addProcedure")
    @ResponseBody
    public int addProcedureForm(HttpServletRequest request) throws IOException {
        String data = request.getParameter("data");
        Object[] arr = JSONObject.parseObject(data,Object[].class);

        ObjectMapper objectMapper = new ObjectMapper();
        MDesignProcedure mDesignProcedure = objectMapper.convertValue(arr[0], MDesignProcedure.class);

        MDesignProcedure mDesignProcedure1 = mDesignProcedureService.queryByProductId(mDesignProcedure.getProductId());
        if(mDesignProcedure1!=null){
            mDesignProcedureService.deleteByPrimaryKey(mDesignProcedure1.getId());
        }
        //通过页面获取dFile的id查询dFile
        DFile  dFile = dFileService.selectByPrimaryKey(Integer.valueOf(arr[1] + ""));
        List<MDesignProcedureDetails> mDesignProcedureDetailList = new ArrayList<MDesignProcedureDetails>();
        for(int i=2;i<arr.length;i++){
            MDesignProcedureDetails mDesignProcedureDetails = objectMapper.convertValue(arr[i], MDesignProcedureDetails.class);
//            System.out.println(mDesignProcedureDetails);
            mDesignProcedureDetailList.add(mDesignProcedureDetails);
        }
        int i = mDesignProcedureService.insertSelective(mDesignProcedure, mDesignProcedureDetailList, dFile);
        return i;
    }
    @PostMapping("queryMDesignProcedure.action")
    @ResponseBody
    public PageVo<MDesignProcedure> queryMDesignProcedure(@RequestBody Map<String,String> map){
        PageVo<MDesignProcedure> pageVo = mDesignProcedureService.queryMDesignProcedure(map);
        return pageVo;
    }
    @PostMapping("checkProcedure.action")
    @ResponseBody
    public int checkProcedure(HttpServletRequest request){
        String data = request.getParameter("data");
        Object[] arr = JSONObject.parseObject(data,Object[].class);

        ObjectMapper objectMapper = new ObjectMapper();
        MDesignProcedure mDesignProcedure = objectMapper.convertValue(arr[0], MDesignProcedure.class);
//        System.out.println(mDesignProcedure);
        List<MDesignProcedureDetails> mDesignProcedureDetailList = new ArrayList<MDesignProcedureDetails>();
        //通过页面获取dFile的id查询dFile
        for(int i=1;i<arr.length;i++){
            MDesignProcedureDetails mDesignProcedureDetails = objectMapper.convertValue(arr[i], MDesignProcedureDetails.class);
//            System.out.println(mDesignProcedureDetails);
            mDesignProcedureDetailList.add(mDesignProcedureDetails);
        }
        int i = mDesignProcedureService.updateByPrimaryKeySelective(mDesignProcedure, mDesignProcedureDetailList);
        //审核不通过时
        if(mDesignProcedure.getProcedureCheckTag().equals("S001-2")){
            DFile dFile = dFileService.queryByProductId(mDesignProcedure.getProductId());
            dFile.setDESIGN_PROCEDURE_TAG("G001-0");
            dFileService.updateByPrimaryKey(dFile);
        }
        return i;
    }

    @PostMapping("/queryAllMDesignProcedure.action")
    @ResponseBody
    public PageVo<MDesignProcedure> queryAllMDesignProcedure(@RequestBody Map<String,String> map){
        PageVo<MDesignProcedure> mDesignProcedurePageVo = mDesignProcedureService.queryAllMDesignProcedure(map);
        return mDesignProcedurePageVo;
    }

    //产品定义人员变更：查询已审核通过的工序组成设计单信息
    @PostMapping("queryGreenMDesignProcedure.action")
    @ResponseBody
    public PageVo<MDesignProcedure> queryGreenMDesignProcedure(@RequestBody Map<String,String> map){
        PageVo<MDesignProcedure> pageVo = mDesignProcedureService.queryGreenMDesignProcedure(map);
        return pageVo;
    }
    //变更
    @PostMapping("changeProcedure.action")
    @ResponseBody
    public int changeProcedure(HttpServletRequest request){
        //修改工序组成设计单信息
        String data = request.getParameter("data");
        Object[] arr = JSONObject.parseObject(data,Object[].class);

        ObjectMapper objectMapper = new ObjectMapper();
        MDesignProcedure mDesignProcedure = objectMapper.convertValue(arr[0], MDesignProcedure.class);
//        System.out.println(mDesignProcedure);
        List<MDesignProcedureDetails> mDesignProcedureDetailList = new ArrayList<MDesignProcedureDetails>();
        //通过页面获取dFile的id查询dFile
        for(int i=1;i<arr.length;i++){
            MDesignProcedureDetails mDesignProcedureDetails = objectMapper.convertValue(arr[i], MDesignProcedureDetails.class);
//            System.out.println(mDesignProcedureDetails);
            mDesignProcedureDetailList.add(mDesignProcedureDetails);
        }
        int i = mDesignProcedureService.changeProcedure(mDesignProcedure, mDesignProcedureDetailList);
        //变更后的物料组成设计单需重新审核:改变状态
        return i;
    }

    @ResponseBody
    @PostMapping("/queryMDesignProcedureModule.action")
    public PageVo<MDesignProcedure> queryMDesignProcedureModule(@RequestBody Map<String,String> map){
        return mDesignProcedureService.queryMDesignProcedureModule(map);
    }
    //点击确认制定工序物料设计单
    @ResponseBody
    @PostMapping("/saveModule.action")
    public int saveModule(String mDesignProcedureStr){
        MDesignProcedure mDesignProcedure = JSONObject.parseObject(mDesignProcedureStr, MDesignProcedure.class);
        //工序物料设计标志:已提交
        mDesignProcedure.setDesignModuleTag("G002-1");
        //审核标志：等待审核
        mDesignProcedure.setModuleCheckTag("S001-0");
//        System.out.println(mDesignProcedure);
        return mDesignProcedureService.updateByPrimaryKeySelective(mDesignProcedure);

    }
    //查询的信息范围为所有尚未通过审核或变更后的工序物料设计单
    @ResponseBody
    @PostMapping("/queryMDesignProcedureModuleNoCheck.action")
    public PageVo<MDesignProcedure> queryMDesignProcedureModuleNoCheck(@RequestBody Map<String,String> map){
        PageVo<MDesignProcedure> pageVo = mDesignProcedureService.queryMDesignProcedureModuleNoCheck(map);
        return pageVo;
    }
    @ResponseBody
    @PostMapping("/checkModule.action")
    public int  checkModule(String mDesignProcedureStr){
        MDesignProcedure mDesignProcedure = JSONObject.parseObject(mDesignProcedureStr, MDesignProcedure.class);
        //工序物料设计标志:G002-2已审核
        mDesignProcedure.setDesignModuleTag("G002-2");
        //如果审核不通过，删除该工序对应的物料信息，并更新物料的可用数量,设置物料设计为未设计
        if(mDesignProcedure.getModuleCheckTag().equals("S001-2")){
            List<DModuleDetails> dModuleDetails = dModuleDetailsService.queryDModuleDetailsByProductId(mDesignProcedure.getProductId());
            for(int i=0;i<dModuleDetails.size();i++){
                DModuleDetails dModuleDetails1 = dModuleDetails.get(i);
                dModuleDetails1.setDesidualAmount(dModuleDetails1.getAmount());
                dModuleDetailsService.updateByPrimaryKeySelective(dModuleDetails1);
            }

            List<MDesignProcedureDetails> mDesignProcedureDetails = mDesignProcedureDetailsService.queryMDesignProcedureDetailsByPId(mDesignProcedure.getId());
            for(int i=0;i<mDesignProcedureDetails.size();i++){
                mDesignProcedureDetails.get(i).setDesignModuleTag("D002-0");
                mDesignProcedureDetailsService.updateByPrimaryKeySelective(mDesignProcedureDetails.get(i));
            }
            mDesignProcedureModuleService.deleteByMDesignProcedureId(mDesignProcedure.getId());
        }
//        System.out.println(mDesignProcedure);
        return mDesignProcedureService.updateByPrimaryKeySelective(mDesignProcedure);
    }
    //查询的信息范围为所有已登记的工序物料设计单
    @ResponseBody
    @PostMapping("/queryMDesignProcedureHasDesign.action")
    public PageVo<MDesignProcedure> queryMDesignProcedureHasDesign(@RequestBody Map<String,String> map){
        PageVo<MDesignProcedure> pageVo = mDesignProcedureService.queryMDesignProcedureHasDesign(map);
        return pageVo;
    }
    //查询的信息范围为所有已登记的工序物料设计单
    @ResponseBody
    @PostMapping("/queryMDesignProcedureIfCheckGreen.action")
    public List<MApply> queryMDesignProcedureIfCheckGreen(String productId, String mApplyIds){
        MDesignProcedure mDesignProcedure = mDesignProcedureService.queryByProductId(productId);
        if(mDesignProcedure==null){
            return null;
        }else{
            if(mDesignProcedure.getModuleCheckTag().equals("S001-1")&&mDesignProcedure.getProcedureCheckTag().equals("S001-1")){
                List<MApply> mApplies = new ArrayList<>();

                Integer[] ids = JSONObject.parseObject(mApplyIds, Integer[].class);
                for(int i=0;i<ids.length;i++){
                    MApply mApply = mApplyService.selectByPrimaryKey(ids[i]);
//                    System.out.println(mApply);
                    mApplies.add(mApply);
                }
                return mApplies;
            }else{
                return null;
            }
        }
    }
}
